﻿using System;
using System.Data;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace mysql
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //与数据库连接的信息
            MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
            //用户名
            builder.UserID = "root";
            //密码
            builder.Password = "12345678";
            //服务器地址
            builder.Server = "localhost";
            //连接时的数据库
            builder.Database = "test";
            //定义与数据连接的链接
            MySqlConnection connection = new MySqlConnection(builder.ConnectionString);
            //打开这个链接
            connection.Open();
            string strcmd = this.textBox1.Text;
            MySqlCommand cmd = new MySqlCommand(strcmd, connection);
            MySqlDataAdapter ada = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            ada.Fill(ds);//查询结果填充数据集
            dataGridView1.DataSource = ds.Tables[0];
            connection.Close();//关闭连接
        }
    }
}
